package com.digital.welcome.controller;


import com.digital.welcome.annotation.OperationLog;
import com.digital.welcome.entity.Dorm;
import com.digital.welcome.service.DormService;
import com.digital.welcome.token.JwtToken;
import com.digital.welcome.util.JwtUtil;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author 韩
 */
@RestController
@RequestMapping("/dorm")
public class DormController {

    @Autowired
    private DormService dormService;

    @OperationLog()
    @ApiOperation("查询寝室")
    @RequiresPermissions("Dorm:choice")
    @RequestMapping(value = "/select", method = RequestMethod.GET)
    public String selectBedRoomByUserId(@RequestHeader JwtToken jwtToken) {
        String token = (String) jwtToken.getPrincipal();
        Claims claims = JwtUtil.parseJWT(token);
        assert claims != null;
        String username = claims.getId();
        return dormService.selectDormByUserName(username);
    }

    @OperationLog()
    @ApiOperation("选寝室")
    @RequiresPermissions("Dorm:choice")
    @RequestMapping(value = "/choice", method = RequestMethod.POST)
    public String insertDorm(@RequestBody Dorm dorm, @RequestHeader JwtToken jwtToken) {
        String token = (String) jwtToken.getPrincipal();
        Claims claims = JwtUtil.parseJWT(token);
        assert claims != null;
        String username = claims.getId();
//        User user = userService.getUserByUsername(username);
        return dormService.insertDormByUser(username, dorm.getDormId());
    }

    @OperationLog()
    @ApiOperation("获取可选寝室号")
    @RequiresPermissions("Dorm:choice")
    @RequestMapping(value = "/selectDormNumber", method = RequestMethod.GET)
    public String selectDormNumber(@RequestHeader JwtToken jwtToken) {
        String token = (String) jwtToken.getPrincipal();
        Claims claims = JwtUtil.parseJWT(token);
        assert claims != null;
        String username = claims.getId();
        return dormService.selectDormNumber(username);
    }

    @OperationLog()
    @ApiOperation("获取可选床位")
    @RequiresPermissions("Dorm:choice")
    @RequestMapping(value = "/getBedNumber", method = RequestMethod.GET)
    public String getBedNumber(String region, String dormNumber) {
        return dormService.getBedNumber(region, dormNumber);
    }

    @OperationLog()
    @ApiOperation("删除已选床位信息")
    @RequiresPermissions("Dorm:choice")
    @RequestMapping(value = "/deleteBedInformation", method = RequestMethod.POST)
    public String deleteBedInformation(@RequestHeader JwtToken jwtToken) {
        String token = (String) jwtToken.getPrincipal();
        Claims claims = JwtUtil.parseJWT(token);
        assert claims != null;
        String username = claims.getId();
        return dormService.deleteBedInformation(username);
    }

    @ApiOperation("首页寝室入住情况")
    @RequestMapping(value = "/dormData", method = RequestMethod.GET)
    public String getBedNumber() {
        return dormService.selectNumber();
    }

}
